define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";

	var wind_async = require('ddf_wind_async');

	var base = require('component-base');
	var myScrollTop=0;
	window.onscroll=function(){	
		function get_scrollTop_of_body(){ //scrollTop能力检测函数
			var scrollTop; 
			if(typeof window.pageYOffset != 'undefined'){ 
				scrollTop = window.pageYOffset; 
			} 
			else 
				if(typeof document.compatMode != 'undefined' && 
						document.compatMode != 'BackCompat'){ 
					scrollTop = document.documentElement.scrollTop; 
				} 
				else 
					if(typeof document.body != 'undefined'){ 
						scrollTop = document.body.scrollTop; 
					} 
			return scrollTop; 
		}
		myScrollTop = parseInt(get_scrollTop_of_body());//获取ScrollTop的值
	}
	/**
	 * 商家列表
	 */
	exports.ddf_dialog_shop = function(id, fn) {

		$(id).ddf_dialog({
			options : {
				url : ddf.shop.queryShops,
				dialog_id : "shopListDiv",
				dialog_List : "shopList",
				page_id : "footPageDiv",
				table_title : "商家列表",
				id_title : "联系方式",
				name_title : "商家名称",
				id_key : "MOBILE",
				name_key : "SHOP_NAME",
				get_id_key : "SHOP_ID",
				search_text_array : [ "联系方式", "商家名称" ],
				search_key_array : [ "mobile", "shopname" ],
				list_key : "shopListData",
				confirmCallBack : fn
			}

		});
	};
	
	/**
	 * 代理商列表
	 */
	exports.ddf_dialog_agent = function(id, fn) {

		$(id).ddf_dialog({
			options : {
				url :ddf.agent.getQueryAgent,
				dialog_id : "agentListDiv",
				dialog_List : "dialogAgentList",
				page_id : "footPageAgentDiv",
				table_title : "代理商列表",
				id_title : "联系方式",
				name_title : "代理商名称",
				id_key : "MOBILE",
				name_key : "AGENT_NAME",
				get_id_key : "AGENT_ID",
				search_text_array : [ "联系方式", "代理商名称" ],
				search_key_array : [ "mobile", "agentname" ],
				list_key : "agentListData",
				confirmCallBack : fn
			}

		});
	};
	
	/**
	 * 商家类型列表
	 */
	exports.ddf_dialog_shop_type = function(id, fn) {

		$(id).ddf_dialog({
			options : {
				url :ddf.shop.getErpShopTypeList,
				dialog_id : "shopTypeListDiv",
				dialog_List : "shopTypeList",
				page_id : "footPageShopTypeDiv",
				table_title : "商家类型列表",
				id_title : "类型代码",
				name_title : "类型名称",
				id_key : "SHOP_TYPE_ID",
				name_key : "SHOP_TYPE_NAME",
				get_id_key : "SHOP_TYPE_ID",
				search_text_array : [ "类型代码", "类型名称" ],
				search_key_array : ["shopTypeId","shopTypeName"],
				list_key : "shopTypeListData",
				confirmCallBack : fn
			}

		});
	};
	
	
	
	/**
	 * 商品单位列表
	 */
	exports.ddf_dialog_mer_unit = function(id, fn) {

		$(id).ddf_dialog({
			options : {
				url : ddf.merchandise.getDUnitsList,
				dialog_id : "merUnitListDiv",
				dialog_List : "merUnitList",
				page_id : "footPageMerUnitDiv",
				table_title : "商品单位列表",
				id_title : "单位代码",
				name_title : "单位名称",
				id_key : "unitCode",
				name_key : "unitName",
				get_id_key : "unitCode",
				search_text_array : [ "单位代码", "单位名称" ],
				search_key_array : [ "unitCode", "unitName" ],
				list_key : "dUnitsList",
				confirmCallBack : fn
			}

		});
	};
	
	
	/**
	 * 银行列表
	 */
	exports.ddf_dialog_bank = function(id, fn) {
		
		$(id).ddf_dialog({
			options : {
				url : ddf.shop.getBanksList,
				dialog_id : "bankListDiv",
				dialog_List : "bankList",
				page_id : "footPageBankDiv",
				table_title : "银行列表",
				id_title : "银行代码",
				name_title : "银行名称",
				id_key : "bankNumber",
				name_key : "bankName",
				get_id_key : "bankNumber",
				search_text_array : [ "银行代码", "银行名称" ],
				search_key_array : [ "bankNumber", "bankName" ],
				list_key : "banksListData",
				confirmCallBack : fn
			}

		});
	};
	
	/**
	 * 群组列表
	 */
	exports.ddf_dialog_group = function(id, fn) {

		$(id).ddf_dialog({
			options : {
				url : ddf.group.getQueryGroup,
				dialog_id : "groupListDiv",
				dialog_List : "groupList",
				page_id : "footPageGroupDiv",
				table_title : "群组列表",
				id_title : "群组代码",
				name_title : "群组名称",
				id_key : "GROUP_ID",
				name_key : "GROUP_NAME",
				get_id_key : "GROUP_ID",
				search_text_array : [ "群组代码", "群组名称" ],
				search_key_array : [ "groupId", "groupName" ],
				list_key : "groupListData",
				confirmCallBack : fn
			}

		});
	};
	
	
	
	/**
	 * 版本列表
	 */
	exports.ddf_dialog_version = function(id, fn) {

		$(id).ddf_dialog({
			options : {
				url : ddf.version.getQueryVersion,
				dialog_id : "versionListDiv",
				dialog_List : "versionList",
				page_id : "footPageVersionDiv",
				table_title : "版本列表",
				id_title : "版本号",
				name_title : "APK名称",
				id_key : "VERSION",
				name_key : "APK_NAME",
				get_id_key : "VERSION_ID",
				search_text_array : [ "版本号", "APK名称" ],
				search_key_array : [ "version", "apkName" ],
				list_key : "versionListData",
				confirmCallBack : fn
			}

		});
	};
	
	/**
	 * 商品父分类和属于某个商家的分类
	 */
/*	exports.ddf_dialog_merchandise_type = function(id, fn) {

		$(id).ddf_dialog({
			options : {
				url : ddf.version.getQueryVersion,
				dialog_id : "erpMerchandiseTypesListDiv",
				dialog_List : "erpMerchandiseTypesList",
				page_id : "footPageErpMerchandiseTypesListDiv",
				table_title : "商品分类列表",
				id_title : "分类代码",
				name_title : "分类名称",
				id_key : "merchandiseTypeId",
				name_key : "merchandiseTypeName",
				get_id_key : "merchandiseTypeId",
				search_text_array : [ "分类代码", "分类名称" ],
				search_key_array : [ "merchandiseTypeId", "merchandiseTypeName" ],
				list_key : "erpMerchandiseTypesList",
				confirmCallBack : fn
			}

		});
	};*/
	

	$.fn.ddf_dialog = function(opts) {

		var panel = $(this);

		var params = {

			"page.pageNumber" : 1,
			"page.pageSize" : 10
		};

		return this
				.each(function() {

					/**
					 * 搜索
					 */
					function search() {

						params[opts.options.search_key_array[0]] = $(
								"#dialog_" + opts.options.search_key_array[0])
								.val();
						params[opts.options.search_key_array[1]] = $(
								"#dialog_" + opts.options.search_key_array[1])
								.val();
						loadList();
					}

					/**
					 * 重置
					 */
					function resetParams() {

						$("#dialog_" + opts.options.search_key_array[0])
								.val("");
						$("#dialog_" + opts.options.search_key_array[1])
								.val("");
						$("#dialog_" + opts.options.search_key_array[2])
								.val("");
					}

					/**
					 * 查询列表
					 */
					function loadList() {

						base.showLoading();

						eval(
								Wind.compile("async", function() {

									var data = $await(wind_async
											.DDF_AsyncRequest("POST",
													opts.options.url, params));

									loadListCallBack(data);

								}))().start();

					}

					/**
					 * 回调函数
					 */
					function loadListCallBack(data) {
						if (data["code"] == 1) {

							$("#" + opts.options.dialog_List).html("");

							for (var i = 0; i < data[opts.options.list_key].length; i++) {
								var htmlStr = "";
								htmlStr += '<tr id="dialog_confirm' + i + '">';
								htmlStr += '<td class="center">'
										+ base.getSequence(data["pageNumber"],
												data["pageSize"], i + 1)
										+ '</td>';
								htmlStr += '<td>'
										+ data[opts.options.list_key][i][opts.options.id_key]
										+ '</td>';
								htmlStr += '<td>'
										+ data[opts.options.list_key][i][opts.options.name_key]
										+ '</td>';
								htmlStr += '<td style="display:none">'
									+ data[opts.options.list_key][i][opts.options.get_id_key]
									+ '</td>';
								

								htmlStr += '</tr> ';

								$("#" + opts.options.dialog_List).append(
										htmlStr);

								var id = data[opts.options.list_key][i][opts.options.get_id_key];

								var name = data[opts.options.list_key][i][opts.options.name_key];
								
								$("#dialog_confirm" + i).on(
										"click",
										function() {

											opts.options.confirmCallBack(
													$(this).find("td:eq(3)")
															.text(), $(this)
															.find("td:eq(2)")
															.text());

											$("#" + opts.options.dialog_id)
													.remove();

											$("body .big_modal-backdrop")
													.remove();

										});
							}

							// 分页
							$('#' + opts.options.page_id).ddf_pagination(
									data["pageSize"],
									data["pageNumber"],
									data["totalCount"],
									data["totalPage"],
									{

										changePageCallback : function(pageSize,
												page) {

											params["page.pageNumber"] = page;

											loadList();
										},
										changePageSizeCallBack : function(
												pageSize) {

											params["page.pageSize"] = pageSize;

											loadList();
										}
									});
						}

						base.cancelLoading();

					}

					/**
					 * 初始化页面元素
					 */
					function init() {

						var divcont = '<div class="big_modal" id="'
								+ opts.options.dialog_id
								+ '" style="margin-top:'+myScrollTop+'px">'
								+ '<div class="modal-header">'
								+ '<button class="close over" type="button">×</button>'
								+ '<h3>'
								+ opts.options.table_title
								+ '</h3>'
								+ '</div>'
								+ '<div>'
								+ '<div class="row-fluid">'
								+ '<div class="span12">'
								+ '<div class="widget-content nopadding">'
								+ '<form class="form-horizontal">'
								+ '<div class="row-fluid">'
								+ '<div class="span6">'
								+ '<div class="control-group">'
								+ '<label class="control-label">'
								+ opts.options.search_text_array[0]
								+ '：</label>'
								+ '<div class="controls">'
								+ '<input class="span8 m-wrap" type="text" id="dialog_'
								+ opts.options.search_key_array[0]
								+ '" />'
								+ '</div>'
								+ '</div>'
								+ '</div>'
								+ '<div class="span6">'
								+ '<div class="control-group">'
								+ '<label class="control-label">'
								+ opts.options.search_text_array[1]
								+ '：</label>'
								+ '<div class="controls">'
								+ '<input class="span8 m-wrap" type="text" id="dialog_'
								+ opts.options.search_key_array[1]
								+ '" />'
								+ '</div>'
								+ '</div>'
								+ '</div>'
								+ '</div>'
								+ '<div class="form-actions">'
								+ '<button class="btn btn-primary" id="searchBtn" type="button" >搜索</button>&nbsp;'
								+ '<button class="btn btn-info" id="resetBtn"  type="button">重置</button>&nbsp;'
								+ '</div>'
								+ '</form>'
								+ '</div>'
								+ '</div>'
								+ '</div>'
								+ '<div class="widget-content nopadding dataTables_wrapper">'
								+ '<table class="table table-bordered data-table">'
								+ '<colgroup>' + '<col width="100px" />'
								+ '</colgroup>' + '<thead>' + '<tr>'
								+ '<th>序号</th>' + '<th>'
								+ opts.options.id_title + '</th>' + '<th>'
								+ opts.options.name_title + '</th>' + '</tr>'
								+ '</thead>' + '<tbody id="'
								+ opts.options.dialog_List + '">' + '</tbody>'
								+ '</table>' + '<div id="'
								+ opts.options.page_id + '"></div>' + '</div>'
								+ '</div>' + '</div>'
								+ '<div class="big_modal-backdrop in"></div>';

						$(divcont).appendTo("body");

						$("#" + opts.options.dialog_id).find(".over").click(
								function() {
									$("#" + opts.options.dialog_id).remove();
									$("body .big_modal-backdrop").remove();
								});
						$("#searchBtn").click(function() {
							search();
						});
						$("#resetBtn").click(function() {
							resetParams();
						});
					}

					init();

					loadList();

				});
	}

});